import os

from .keyboard import Keyboard


class Contacts:
    ability_name = 'com.ohos.contacts.MainAbility'
    bundle_name = 'com.ohos.contacts'
    window_name = 'contacts'

    add_contact_btn = (612, 114)

    test_name = 'a'
    test_company = 'c'
    test_number = '012'

    detail_btn = (662, 328)

    mms_link = (666, 645)

    btn_1 = (144, 680)
    btn_2 = (360, 680)
    btn_3 = (576, 680)
    btn_4 = (144, 770)
    btn_5 = (360, 770)
    btn_6 = (576, 770)
    btn_7 = (144, 860)
    btn_8 = (360, 860)
    btn_9 = (576, 860)
    btn_xing = (144, 950)
    btn_0 = (360, 950)
    btn_jin = (576, 950)
    btn_plane = (144, 1045)
    btn_call = (360, 1045)
    btn_delete = (576, 1045)
    # 底部的电话、联系人、收藏 按钮
    menu_phone = (120, 1156)
    menu_contacts = (360, 1156)
    menu_favor = (600, 1156)

    BASE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'snapshot', 'contacts')

    standard_del_menu = os.path.join(BASE_DIR, 'contacts_del_menu.jpeg')

    @classmethod
    def create_contactor(cls, device):
        cls.delete_contactor(device)
        device.start_ability(cls.bundle_name, cls.ability_name)
        device.click(*cls.menu_contacts)
        device.click(*cls.add_contact_btn)
        device.refresh_layout()
        device.click_element(device.get_element_by_condition(condition={'hint': '姓名'}))
        device.click(*Keyboard.key_a)
        device.click(*Keyboard.close)

        device.click_element(device.get_element_by_condition(condition={'hint': '公司'}))
        device.click(*Keyboard.key_c)
        device.click(*Keyboard.close)

        device.refresh_layout()
        number_area = device.get_element_by_condition(condition={'hint': '电话号码'})
        device.click_element(number_area)
        device.input_text(270, 605, cls.test_number)
        device.click(*Keyboard.close)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('AddContact_OK'))
        device.go_back()
        device.force_stop(cls.bundle_name)

    @classmethod
    def delete_contactor(cls, device):
        device.force_stop(cls.bundle_name)
        device.hdc_shell(' rm -rf /data/app/el2/100/database/com.ohos.contactsdataability/*')
